package com.aternity.httpfetch;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author Daniel Orr
 */
public class Fetcher1 {

    public static void main(String[] args) throws IOException {
        HttpClient client = new HttpClient();
        String[] countryCodes = Locale.getISOCountries();
        File f = new File("ip.list.csv");
        if(f.exists()){
            FileUtils.forceDelete(f);
        }
        for (String countryCode : countryCodes) {
            System.out.println("countryCode="+countryCode);
            HttpMethod method = new GetMethod("http://www.nirsoft.net/countryip/"+countryCode.toLowerCase()+".csv");
            try {
                client.executeMethod(method);
                final InputStream is = method.getResponseBodyAsStream();
                final String s = IOUtils.toString(is);
                if(! (s.endsWith("\n") || s.endsWith("\r"))){
                    System.out.println("-");
                }
                if(!s.contains("<title>404 Not Found</title>")){
                    FileUtils.write(f, s, true);
                }
            } catch (IOException ex) {
                Logger.getLogger(Fetcher1.class.getName()).log(Level.SEVERE, null, ex);
            }
             
        }
        
    }
}
